package misp_backend.service;

import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.IdUtil;
import com.google.common.base.Strings;
import misp_backend.domain.FileInfo;
import misp_backend.domain.SysOss;
import misp_backend.storage.MinioStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import misp_backend.storage.storage;
import java.io.InputStream;
import java.util.Date;

@Service
public class FileService {
    // oss服务商 minio/aliyun
    private final String provider = "minio";
    @Autowired
    private SysOssService sysOssService;
    // 存储桶名称
    private final String bucketName = "misp-minio";
    @Autowired
    @Qualifier("minioStorage")
 private storage storage;
    /**
     * 上传文件
     * @param file
     * @return
     */
    public FileInfo uploadFile(MultipartFile file,Long dishId) {
        FileInfo fileInfo = getFileInfo(file);
        try {
            storage.putObject(fileInfo.getBucketName(), fileInfo.getFileName(), file.getInputStream(), fileInfo.getContentType());
            SysOss sysOss=new SysOss();
            sysOss.setBucket(fileInfo.getBucketName());
            sysOss.setFileName(fileInfo.getFileName());
            sysOss.setStorage(provider);
            sysOss.setFileUrl(getUrlByName(fileInfo.getFileName()));
            sysOss.setContentType(file.getContentType());
            sysOss.setFileSize(fileInfo.getFileSize());
            sysOss.setCreateTime(new Date());
            sysOss.setDishId(dishId);
            sysOssService.insert(sysOss);
            return fileInfo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 根据名称得到文件对象流
     * @param objectName
     * @return
     */
    public InputStream getFileStream(String objectName) {
        try {
            return storage.getObject(bucketName, objectName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 根据名称得到文件url
     * @param objectName
     * @return
     */
    public String getUrlByName(String objectName) {
        try {
            return storage.getObjectUrl(bucketName, objectName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 根据名称删除文件
     * @param objectName
     */
    public void removeFileByName(String objectName) {
        try {
            storage.removeObject(bucketName,objectName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 得到上传文件的信息
     * @param file
     * @return
     */
    private FileInfo getFileInfo(MultipartFile file){
        final String fileOriginalName = file.getOriginalFilename();
        String objectName = IdUtil.fastSimpleUUID();
        String fileName = objectName;
        String fileSuffix = fileOriginalName.substring(fileOriginalName.lastIndexOf(".") + 1);
        if (!Strings.isNullOrEmpty(fileSuffix)) {
            fileName = objectName.concat(String.valueOf(CharUtil.DOT)).concat(fileSuffix);
        }
        FileInfo fileInfo = new FileInfo();
        fileInfo.setFileName(fileName);
        fileInfo.setFileSize(file.getSize());
        fileInfo.setContentType(file.getContentType());
        fileInfo.setStorage(provider);
        fileInfo.setBucketName(bucketName);
        return fileInfo;
    }
}
